package com.example.admin.brainwave.utils;

import android.content.Context;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import android.util.Log;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

public class FileHelper {

    private Context context;


    public FileHelper(Context context) {
        this.context = context;
    }


    /**
     * 写入内容到SD卡中的txt文本中
     * str为内容
     */
    public void writeSDFile(String strcontent, String fileName) {
        String folderName = "Log";
        File sdCardDir = new File(Environment.getExternalStorageDirectory(), folderName);
        if (!sdCardDir.exists()) {
            if (!sdCardDir.mkdirs()) {
                try {
                    sdCardDir.createNewFile();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        // 每次写入时，都换行写
        String strContent = strcontent + "\r\n";
        try {
            File saveFile = new File(sdCardDir, fileName);
            if (!saveFile.exists()) {
                saveFile.createNewFile();
            }
            FileOutputStream outStream = new FileOutputStream(saveFile);
            outStream.write(strContent.getBytes());
            outStream.close();
        } catch (Exception e) {
            Log.e("TestFile", "Error on write File:" + e);
        }
    }


}
